﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CMS
{
    public class MoveNews : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "MoveNews";
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Title","Chuyển tin tức");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Description","");
            }
        }


        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(MoveNews));
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);

                string NewsId = (string)Environment.Environments.Request("NewsId");
                
                NewsCls NewsCls=new NewsBLL().CreateModel(Environment.Environments.ActiveLanguage,NewsId);
                DataTable dtGroupNewsItem = new GroupNewsBLL().ReadingGroupNews();
                string FirstGroupNewsId = "";
                string SelectText = "<select id=drpSelectGroupNews onchange=\"javascript:ClientDetectGroupNewsItem();\">";
                for (int iIndex = 0; iIndex < dtGroupNewsItem.Rows.Count; iIndex++)
                {
                    string GroupNewsId = (string)dtGroupNewsItem.Rows[iIndex]["GroupNewsId"];
                    string Subject = (string)dtGroupNewsItem.Rows[iIndex][Environment.Environments.ActiveLanguage + "Subject"];
                    SelectText += "<option value=\"" + GroupNewsId + "\">" + Subject + "</option>";
                    if (iIndex == 0) FirstGroupNewsId = GroupNewsId;
                }
                SelectText += "</select><input type=button value=\"...\" onclick=\"javascript:ClientDetectGroupNewsItem();\">";
                string RetHtml =
                    WebPartUtilities.AttachJavascript("Portal.CMS", "News.js") +
                                 Environment.Environments.GetFormDialog(
                                 "      <table>" +
                                 "          <tr>" +
                                 "              <td colspan=2>"+NewsCls.Subject+"</td>" +
                                 "          </tr>" +
                                 "          <tr>" +
                                 "              <td>" + ResourceBLL.GetText(WebPartId, "MoveNewsCaption", "Chuyển sang nhóm tin khác") + "</td>" +
                                 "              <td>"+SelectText+"</td>" +
                                 "          </tr>" +
                                 "          <tr>" +
                                 "              <td>" + ResourceBLL.GetText(WebPartId, "MoveNewsItemCaption", "Nhóm chuyển") + "</td>" +
                                 "              <td><div id=divSelectGroupNewsItems>" + ServerSideDetectGroupNewsItem(FirstGroupNewsId) + "</div></td>" +
                                 "          </tr>" +
                                 "          <tr>" +
                                 "              <td></td>" +
                                 "              <td><div id=divProcessing></div></td>" +
                                 "          </tr>" +
                                 "          <tr>" +
                                 "              <td></td>" +
                                 "              <td><input type=button class=cssSaveButton onclick=javascript:ClientExecuteMoveNews('"+NewsId+"','"+NewsCls.GroupNewsItemId+"'); value=\"" + ResourceBLL.GetText(WebPartId, "MoveNewsSaveButton", "Ghi lại") + "\"><input type=button class=cssCancelButton onclick=javascript:window.close(); value=\"" + ResourceBLL.GetText(WebPartId, "MoveNewsCancelButton", "Thoát") + "\"></td>" +
                                 "          </tr>" +
                                 "      </table>", Title);

                
                return RetHtml;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideDetectGroupNewsItem(string GroupNewsId)
        {
            DataTable dtGroupNewsItem = new GroupNewsBLL().ReadingGroupNewsItemsForMove(GroupNewsId);
            string SelectText = "<select id=drpSelectGroupNewsItem>";
            for (int iIndex = 0; iIndex < dtGroupNewsItem.Rows.Count; iIndex++)
            {
                string GroupNewsItemId = (string)dtGroupNewsItem.Rows[iIndex]["GroupNewsItemId"];
                string GroupNewsItemName = (string)dtGroupNewsItem.Rows[iIndex][Environment.Environments.ActiveLanguage + "GroupNewsItemName"];
                SelectText += "<option value=\"" + GroupNewsItemId + "\">" + GroupNewsItemName + "</option>";
            }
            SelectText += "</select>";
            return SelectText;
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideMoveNews(string NewsId,string MoveToGroupNewsItemId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                NewsCls NewsCls = new NewsBLL().CreateModel(Environment.Environments.ActiveLanguage, NewsId);
                if (NewsCls.GroupNewsItemId.Equals(MoveToGroupNewsItemId))
                {
                    throw new Exception(ResourceBLL.GetText(WebPartId,"NotMoveSameGroup","Không chuyển cùng nhóm!"));
                }
                NewsCls.GroupNewsItemId=MoveToGroupNewsItemId;
                new NewsBLL().SaveNews(null, Environment.Environments.ActiveLanguage, MoveToGroupNewsItemId, NewsId, NewsCls.Subject, NewsCls.ImageUrl, NewsCls.ShortContent, NewsCls.Body, NewsCls.SearchKeyword, NewsCls.Source, NewsCls.EntryDate, NewsCls.HasDocAttached, NewsCls.HasVideoAttached, NewsCls.HasImageAttached, NewsCls.EmbVideo, NewsCls.EmbAudio);
                return ResourceBLL.GetText(WebPartId,"MoveNewsOk","Chuyển thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string GetConfirmMoveMsg()
        {
            return ResourceBLL.GetText(WebPartId, "ConfirmMoveMsg", "Bạn đã chắc chắn chuyển lại dữ liệu tin tức chưa?");
        }

        [AjaxPro.AjaxMethod]
        public string GetProcessingMove()
        {
            return ResourceBLL.GetText(WebPartId, "ProcessingMove", "Đang chuyển dữ liệu ...");
        }

    }
}